#/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtGui, QtCore
from .painter.paintimage import get_text_image, get_mask_pixmap
from .pyuic.user_widget import Ui_user_widget

class UserWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(UserWidget, self).__init__(parent)
        self._ui = Ui_user_widget()
        self._ui.setupUi(self)
        pix = get_mask_pixmap(':/icons/ver_logo.png', ':/icons/mask.png', (200,200))
        self._ui.toolButton_2.setIcon(QtGui.QIcon(pix))
        self.project_changed = self._ui.project_box.currentIndexChanged
        self.partment_changed = self._ui.part_box.currentIndexChanged
        self.setStyleSheet("""QComboBox{background-color:rgba(255,255,255,0);border-style:none;outline:0px;}
                                QComboBox::drop-down{
                                    border-style: none;
                                                    }
                                QAbstractItemView
                                {
                                    border: 1px solid rgb(0, 120, 210);
                                    outline:0px;
                                    background:rgb(255, 255, 255);
                                    color:black;
                                }
                                                    """)
        self._ui.project_box.setStyleSheet("""
                                            font:12pt \"Arial Black\"
                                            """
        )
        self._ui.part_box.setStyleSheet("""
                                            font:75 9pt \"Arial\"
                                            """
                                           )
        # self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
    def clear_data(self):
        self._ui.project_box.clear()
        self._ui.part_box.clear()

    def set_project_icon(self, image_path):
        pix = get_mask_pixmap(image_path, ':/icons/mask.png', (200,200))
        self._ui.toolButton_2.setIcon(QtGui.QIcon(pix))

    def add_project_item(self, text):
        self._ui.project_box.addItem(text)

    def add_partment_item(self, text):
        self._ui.part_box.addItem(text)

    def clear_partment_item(self):
        self._ui.part_box.clear()

    def current_project(self):
        return self._ui.project_box.currentText()

    def current_partment(self):
        return self._ui.part_box.currentText()

    def set_current_project(self, index):
        self._ui.project_box.setCurrentIndex(index)

    def set_current_partment(self, index):
        self._ui.part_box.setCurrentIndex(index)

    def find_project(self, text):
        return self._ui.project_box.findText(text)

    def find_partment(self, text):
        return self._ui.part_box.findText(text)

    def get_round_image(self, size):
        result_image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
        head_image = QtGui.QImage('xx.png')
        head_image.scaled(size)
        head_mask = QtGui.QImage('mask.png')

        painter = QtGui.QPainter(result_image)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
        painter.fillRect(result_image.rect(), QtCore.Qt.transparent)

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
        painter.drawImage(0, 0, head_mask)

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
        painter.drawImage(0, 0, head_image)

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
        painter.end()

        return result_image

    def get_round_pixmap(self, pix, size):
        result_image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
        head_mask = QtGui.QPixmap(':/icons/mask.png')

        painter = QtGui.QPainter(result_image)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
        painter.fillRect(result_image.rect(), QtCore.Qt.transparent)

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
        painter.drawPixmap(0, 0, head_mask.scaled(size))

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
        painter.drawPixmap(0, 0, pix.scaled(size))

        painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
        painter.end()

        pix = QtGui.QPixmap.fromImage(result_image)
        return pix

